<template>
  <div class="app-container">
    <span style="font-size: medium;color: rgba(151,170,195) "> / 采购计划 / 需求预测</span>

    <el-form :inline="true" label-width="100px" style="margin-top: 20px">
      <el-form-item label="需求预测周期" prop="planCycle">
        <el-date-picker
            v-model="planCycle"
            type="month"
            placeholder="Pick a month"
            value-format="YYYY-MM"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="demandForecast">需求预测</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="planList"
              style="margin-top: 15px; height: 80%">
      <el-table-column label="采购计划编号" fixed align="center" prop="planNo" width="150px"/>
      <el-table-column label="货品编码" align="center" prop="code"/>
      <el-table-column label="货品名称" align="center" prop="rawProductName"/>
      <el-table-column label="需求日期" align="center" prop="demandDate">
        <template #default="scope">
          <span>{{ parseTime(scope.row.demandDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划购买数量(吨)" align="center" width="200px">
        <template #default="scope">
          <el-input-number v-model="scope.row.quantity" controls-position="right"/>
        </template>
      </el-table-column>
      <el-table-column label="预算(元)" align="center" prop="budget" >
        <template #default="scope">
          <el-input-number v-model="scope.row.budget" controls-position="right" style="width: 90%"/>
        </template>
      </el-table-column>
      <el-table-column label="预期交货时间" align="center" prop="expectedDeliveryTime">
        <template #default="scope">
          <el-date-picker
              v-model="scope.row.expectedDeliveryTime"
              type="Date"
              value-format="YYYY-MM-DD"
              style="width: 90%;"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleAddRemark(scope.row)">添加备注</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="checkSubmit"
    />

    <!-- 添加备注 -->
    <el-dialog title="添加备注" v-model="isVisible" width="800px" append-to-body>
      <el-form label-width="80px">
        <el-form-item label="备注">
          <el-input type="textarea" v-model="remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="confirmRemarks">确 定</el-button>
          <el-button @click="cancelRemarks">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <div style="display: grid; place-items: center;
                margin-top: 10px; margin-bottom: 20px">
      <el-button
          v-if="isShowSave"
          type="primary"
          @click="handleGenerate"
      >生成采购计划</el-button>
    </div>
  </div>
</template>

<script setup name="DemandForecast">
import {parseTime} from "@/utils/ruoyi.js";
import {confirmPurchasePlan, listLatestPurchasePlan, planDemandForecast} from "@/api/purchase/purchasePlan.js";
import {useRouter} from "vue-router";

const { proxy } = getCurrentInstance();
const router = useRouter();

//数据表格展示
const planList = ref([]);
const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  rawProductName: null,
  demandDate: null,
})
//备注
const isVisible = ref(false);
const remarks = ref("");
const currentPlanNo = ref("");
const planCycle = ref("2025-03");
const isShowSave = ref(false);//是否展示保存按钮

/** 查询采购计划列表 */
function getList() {
  loading.value = true;
  listLatestPurchasePlan(queryParams.value).then(response => {
    planList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function checkSubmit() {
  proxy.$modal.confirm('是否保存当前计划信息？').then(function() {
    confirmPurchasePlan(planList.value).then(response => {
      proxy.$modal.msgSuccess("保存成功");
      getList();
    });
  }).catch(() => {});
}

function cancelRemarks() {
  isVisible.value = false;
  remarks.value = '';
}

function confirmRemarks(){
  isVisible.value = false;
  planList.value.forEach(item => {
    if (item.planNo === currentPlanNo.value) {
      item.remarks = remarks.value;
    }
  });
  proxy.$modal.msgSuccess("保存成功");
}

function handleAddRemark(row){
  isVisible.value = true;
  currentPlanNo.value = row.planNo;
}

function handleGenerate() {
  confirmPurchasePlan(planList.value).then(response => {
    proxy.$modal.msgSuccess("保存成功");
    setTimeout(() => {
      proxy.$tab.closeOpenPage(router.currentRoute.value);
      proxy.$router.push({
        path:'/Purchase/PurchasePlan/ForecastShow',
      });
    },500)
  });
}

function demandForecast() {
  const cycleNo = `PP${planCycle.value.replace(/-/g, "")}`
  planDemandForecast(cycleNo).then(response => {
    isShowSave.value = true;
    getList();
  })
}

</script>